#include "../inc/client.h"

int main(int argc, char const *argv[])
{
    char target_ip[256];      // 目标ip
    char private_msg[256];    // 私聊消息
    char buffer[BUFFER_SIZE]; // 传输数据
    char server_exit[100];  // 服务器退出
    int flg = 0;
    // 创建套接字
    int cli_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (cli_fd == -1)
    {
        perror("socket");
        return -1;
    }
    // 连接服务器
    struct sockaddr_in ser_info;
    memset(&ser_info, 0, sizeof(ser_info));
    ser_info.sin_family = AF_INET;
    ser_info.sin_port = htons(8888);
    ser_info.sin_addr.s_addr = inet_addr("192.168.53.153");

    if (connect(cli_fd, (struct sockaddr *)&ser_info, sizeof(ser_info)) == -1)
    {
        perror("connect");
        return -1;
    }
    else
    {
        printf("欢迎来到Penguin Chat loading ......\n");
    }
    int num = 0;
    loop:
    while (1)
    {   
        // 菜单======================================================================================
        sleep(3);
        system("clear");
        printf("🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐  Penguin-Chat  🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐\n");
        printf("  🌐                                                              🌐\n");
        printf("    🌐       👉👉👉👉👉👉👉👉👉功能选择👈👈👈👈👈👈👈👈👈       🌐\n");
        printf("      🌐       👉👉👉👉👉👉                👈👈👈👈👈👈       🌐\n");
        printf("        🌐       👉👉👉👉👉    >1< 私聊    👈👈👈👈👈       🌐\n");
        printf("          🌐       👉👉👉👉    >2< 群聊    👈👈👈👈       🌐\n");
        printf("            🌐       👉👉👉    >3< 天气    👈👈👈       🌐\n");
        printf("          🌐       👉👉👉👉    >4< 在线    👈👈👈👈       🌐\n");
        printf("        🌐       👉👉👉👉👉    >5< 退出    👈👈👈👈👈       🌐\n");
        printf("      🌐       👉👉👉👉👉👉                👈👈👈👈👈👈       🌐\n");
        printf("    🌐       👉👉👉👉👉👉👉👉👉功能选择👈👈👈👈👈👈👈👈👈       🌐\n");
        printf("  🌐                                                              🌐");
        printf("\n🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐  Penguin-Chat  🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐🌐\n");
        printf("请输入你的选择：");

        scanf("%d", &num);

        switch (num)
        {
        case 1: // 私聊===============================================================================
            Private_Task(&cli_fd);
            break;
        case 2: // 群聊===============================================================================
            Broadcast_Task(&cli_fd);
            break;
        case 3: // 天气===============================================================================
            if (write(cli_fd, SHOW_WEATHER, strlen(SHOW_WEATHER)) == -1)
            {
                perror("write ...");
                return -1;
            }
            //请输入要查询的城市=======================================================================
            system("clear");
            printf("请输入要查询的城市\n");
            scanf("%s", buffer);
            //发送查询的城市
            if (write(cli_fd, buffer, strlen(buffer)) == -1)
            {
                perror("write ...");
                return -1;
            }
            memset(buffer, 0, sizeof(buffer));
            //接收天气信息
            if (read(cli_fd, buffer, sizeof(buffer)) == -1)
            {
                perror("read ...");
                return -1;
            }
            system("clear");
            printf("💠💠💠💠💠💠天气情况💠💠💠💠💠💠\n");
            printf("\n%s\n", buffer);
            printf("💠💠💠💠💠💠💠💠💠💠💠💠💠💠💠💠\n");
            memset(buffer, 0, sizeof(buffer));
            goto loop;

            break;

        case 4: // 在线===============================================================================
            if (write(cli_fd, SHOW_ONLINE_CLI, strlen(SHOW_ONLINE_CLI)) == -1)
            {
                perror("write ...");
                return -1;
            }
            if (read(cli_fd, buffer, sizeof(buffer)) == -1)
            {
                perror("read ...");
                return -1;
            }
            system("clear");
            printf("💠💠💠💠💠💠在线人员💠💠💠💠💠💠\n");
            printf("\n%s\n", buffer);
            printf("💠💠💠💠💠💠💠💠💠💠💠💠💠💠💠💠\n");
            memset(buffer, 0, sizeof(buffer));
            goto loop;
            break;
        case 5: // 退出===============================================================================
            if (write(cli_fd, EXIT_MASK, strlen(EXIT_MASK)) == -1)
            {
                perror("write ...");
                
                return -1;
            }
            close(cli_fd);
            return 0;
            break;

        default:
            break;
        }
    }

    close(cli_fd);

    return 0;
}